package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import pojo.Operator;

import common.RowMapper;
import common.SQLTemplate;

import dao.OperatorDAO;

public class OperatorDAOImpl extends BaseDAO implements OperatorDAO {

	@Override
	public Operator getOperatorById(String operatorId) {
		String sql = SQLTemplate.OPERATOR_SELECT_JOIN_ADMIN_RIGHT_SQL
				+ "AND OPERATOR_ID='" + operatorId + "';";
		Operator operator = null;
		try {
			operator = (Operator) queryForObject(sql, new OperatorRowMapper());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return operator;
	}

	@Override
	public List<Operator> getOperators() {
		String sql = SQLTemplate.OPERATOR_SELECT_JOIN_ADMIN_RIGHT_SQL + ";";
		List<Operator> operators = null;
		try {
			operators = (List<Operator>) queryForList(sql,
					new OperatorRowMapper());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return operators;
	}

	@Override
	public Operator add(Operator operator) {
		String tableName = "T_CUSTOMER";
		String[] columnNames = { "ID", "NAME", "SEX", "AGE", "IDENTITY_CARD",
				"PASSWORD", "MOBILE_PHONE", "ADDRESS", "BANK_CARD", "SALARY",
				"ADMIN_ID" };
		String[] values = { operator.getId(), operator.getName(),
				operator.getSex(), operator.getAge(),
				operator.getIdentityCard(), operator.getPassword(),
				operator.getMobilePhone(), operator.getAddress(),
				operator.getBankCard(), operator.getSalary(),
				operator.getAdminId() };
		String sql = SQLTemplate.addSQLTemplate(tableName, columnNames, values);
		try {
			insert(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return operator;
	}

	@Override
	public Operator update(Operator operator) {
		String tableName = "T_CUSTOMER";
		String[] columnNames = { "NAME", "SEX", "AGE", "IDENTITY_CARD",
				"PASSWORD", "MOBILE_PHONE", "ADDRESS", "BANK_CARD", "SALARY",
				"ADMIN_ID" };
		String[] values = { operator.getName(), operator.getSex(),
				"" + operator.getAge(), operator.getIdentityCard(),
				operator.getPassword(), operator.getMobilePhone(),
				operator.getAddress(), "" + operator.getBankCard(),
				operator.getSalary(), operator.getAdminId() };
		String sql = SQLTemplate.updateSQLTemplate(tableName, columnNames,
				values) + "WHERE ID='" + operator.getId() + "';";
		try {
			update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return operator;
	}

	private class OperatorRowMapper implements RowMapper<Object> {
		private OperatorRowMapper() {
		}

		public Operator mapRow(ResultSet rs, int rowNum) throws SQLException {
			String id = rs.getString("id");
			String name = rs.getString("name");
			String sex = rs.getString("sex");
			String age = rs.getString("age");
			String identityCard = rs.getString("identity_card");
			String password = rs.getString("password");
			String mobilePhone = rs.getString("mobile_phone");
			String address = rs.getString("address");
			String bankCard = rs.getString("bank_card");
			String salary = rs.getString("salary");
			String rights = rs.getString("rights");
			String adminId = rs.getString("admin_id");
			String adminName = rs.getString("admin_name");
			Operator operator = new Operator();
			if (id != null) {
				operator.setId(id);
			}
			if (name != null) {
				operator.setName(name);
			}
			if (sex != null) {
				operator.setSex(sex);
			}
			if (age != null) {
				operator.setAge(age);
			}
			if (identityCard != null) {
				operator.setIdentityCard(identityCard);
			}
			if (password != null) {
				operator.setPassword(password);
			}
			if (mobilePhone != null) {
				operator.setMobilePhone(mobilePhone);
			}
			if (address != null) {
				operator.setAddress(address);
			}
			if (bankCard != null) {
				operator.setBankCard(bankCard);
			}
			if (salary != null) {
				operator.setSalary(salary);
			}
			if(rights!=null){
				operator.setRights(salary);
			}
			if (adminId != null) {
				operator.setAdminId(adminId);
			}
			if (adminName != null) {
				operator.setAdminName(adminName);
			}
			return operator;
		}
	}
}
